package com.uk.ui.fatura;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.uk.data.containers.TarifaFaturaContainer;
import com.uk.data.ejbs.IFaturaBean;
import com.uk.data.entities.Fatura;
import com.uk.data.entities.Tarifa;
import com.uk.myclasses.ATable;
import com.uk.reports.ReportWindow;
import com.uk.reports.Reports;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeButton;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class FaturaView extends VerticalLayout implements Button.ClickListener{
	private KontrataInfo kontraInfo = new KontrataInfo();
	
	private Fatura fatura;

	private IFaturaBean faturaBean;
	private TarifaFaturaContainer tarifaFaturaContainer;
	final ATable tarifaListTable = new ATable();
	
	final Button mbyllFature = new Button("Mbyll fature");
	final Button printoFature = new Button("Printo fature");
	final NativeButton anulloFature = new NativeButton("Printo fature");
	
	
	public FaturaView(final Fatura fatura, IFaturaBean faturaBean, Object selectedMenu){
		setMargin(true);
		setSpacing(true);
		this.fatura = fatura;
		this.faturaBean = faturaBean;
		kontraInfo.loadKontrata(fatura.getKontrata());
		this.addComponent(kontraInfo);
		this.addComponent(loadLeximiInfo());
		this.addComponent(this.loadTarifaInfo());
		this.setSasiaOnTarifaListTable(fatura.getLeximiAktual() - fatura.getLeximiKaluar() + fatura.getHarxhimiAfrofe());
		
		if (FaturaNavigationTree.MBYLL.equals(selectedMenu)){
			this.addComponent(this.mbyllFature);
			this.mbyllFature.addListener(this);
		}
		else if (FaturaNavigationTree.PRINTO.equals(selectedMenu)){
			this.addComponent(this.printoFature);
			this.printoFature.addListener(new Button.ClickListener() {
				
				@Override
				public void buttonClick(ClickEvent event) {
					getApplication().getMainWindow().addWindow(new ReportWindow(getApplication(), Reports.buildFaturaReport(fatura))) ;
					
				}
			});
		}
		else if (FaturaNavigationTree.ANULLO.equals(selectedMenu)){
			this.addComponent(this.anulloFature);
			this.anulloFature.addListener(this);
		}
			
		
		
		
	}
	
	
	
	private HorizontalLayout loadLeximiInfo(){
		
		
		final GridLayout gridLayout = new GridLayout(3,6);
		gridLayout.setWidth(600, Sizeable.UNITS_PIXELS);
		gridLayout.setHeight(150, Sizeable.UNITS_PIXELS);
		
		final Label periudhaLabel = new Label("<strong>PERIUDHA</strong>",Label.CONTENT_XHTML);
	
		gridLayout.addComponent(periudhaLabel);

		final Label dataLabel = new Label("<strong>DATA</strong>",Label.CONTENT_XHTML);
		gridLayout.addComponent(dataLabel);

		final Label leximiLabel = new Label("<strong>LEXIMI</strong>",Label.CONTENT_XHTML);
		gridLayout.addComponent(leximiLabel);
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
		
		gridLayout.addComponent(new Label("Muaji aktual"));
		gridLayout.addComponent(new Label(sdf.format(fatura.getDataLeximAktual())));
		gridLayout.addComponent(new Label(Double.toString(fatura.getLeximiAktual())));

		gridLayout.addComponent(new Label("Muaji kaluar"));
		gridLayout.addComponent(new Label(sdf.format(fatura.getDataLeximKaluar())));
		gridLayout.addComponent(new Label(Double.toString(fatura.getLeximiKaluar())));
		
		
		final Label harxhimiLabel = new Label("<strong>Harxhimi</strong>", Label.CONTENT_XHTML);
		gridLayout.addComponent(harxhimiLabel,0, 3, 1, 3);
		String harxhimi = Double.toString(fatura.getLeximiAktual() - fatura.getLeximiKaluar());
		gridLayout.addComponent(new Label(harxhimi), 2,3);
		
		final Label harxhimiAfrofeLabel = new Label("<strong>Harxhimi Afrore</strong>", Label.CONTENT_XHTML);
		gridLayout.addComponent(harxhimiAfrofeLabel,0, 4, 1, 4);
		gridLayout.addComponent(new Label(Double.toString(fatura.getHarxhimiAfrofe())), 2, 4);
		
		
		gridLayout.addComponent(new Label("<strong>Harxhimi total</strong>", Label.CONTENT_XHTML), 0, 5, 1, 5);
		
		gridLayout.addComponent(new Label(Double.toString(fatura.getLeximiAktual() - fatura.getLeximiKaluar()) + fatura.getHarxhimiAfrofe()), 2, 5);
	
		
		gridLayout.setStyleName("gridexample");
		gridLayout.setSpacing(true);
		
		HorizontalLayout hl = new HorizontalLayout();
		hl.setSpacing(true);
		hl.addComponent(gridLayout);
		
		
		return hl;
		
	}
	
	private ATable loadTarifaInfo(){
		
		Collection<Tarifa> col = fatura.getTarifaCollection();
		
		double totali = 0;
		
		List<TarifaFatura> tarifaFaturas = new ArrayList<TarifaFatura>();
		
		
		
		for (Iterator iterator = col.iterator(); iterator.hasNext();) {
			Tarifa tarifa = (Tarifa) iterator.next();
			
			TarifaFatura tarifaFatura = new TarifaFatura();
			tarifaFatura.setId(tarifa.getId());
			tarifaFatura.setKodi(tarifa.getKodi());
			tarifaFatura.setPershkrimi(tarifa.getPershkrimi());
			tarifaFatura.setNjesia(tarifa.getNjesia());
			
			tarifaFatura.setCmimi(tarifa.getCmimi());
			tarifaFatura.setAplikoTvsh(tarifa.isAplikoTVSH());
			
			tarifaFaturas.add(tarifaFatura);
			
			double sasia = 0;
			
			if (tarifa.getNjesia() != null && tarifa.getNjesia().toLowerCase().equals("m3")){
				sasia = 0;
			}
			else{
				sasia = 1;
			}
	
			tarifaFatura.setSasia(sasia);

			double vlera =  sasia * tarifa.getCmimi();
			if (tarifa.isAplikoTVSH()){
				vlera = vlera + vlera*20/100; 
			}

			totali += vlera;
			tarifaFatura.setVlera(vlera);
		}
		 tarifaFaturaContainer = new TarifaFaturaContainer(TarifaFatura.class,tarifaFaturas );
		
		
		
		
		tarifaListTable .setContainerDataSource(tarifaFaturaContainer);
		tarifaListTable.setPageLength(4);
	
		tarifaListTable.setSelectable(true);
		tarifaListTable.setImmediate(true);
		tarifaListTable.setSortDisabled(true);
		tarifaListTable.setWidth("600px");
		tarifaListTable.setFooterVisible(true);
		tarifaListTable.setColumnFooter("kodi", "Totali");

		tarifaListTable.setVisibleColumns(new String[]{  "kodi","pershkrimi", "njesia", "sasia", "cmimi", "aplikoTvshString", "vlera"});
		tarifaListTable.setColumnHeaders(new String[]{ "Kodi","Pershkrimi", "Njesia", "Sasia", "Cmimi", "Apliko TVSH", "Vlera"});
		tarifaListTable.setColumnAlignment("sasia",Table.ALIGN_RIGHT );
		tarifaListTable.setColumnAlignment("cmimi",Table.ALIGN_RIGHT );
		tarifaListTable.setColumnAlignment("tvsh",Table.ALIGN_RIGHT );
		tarifaListTable.setColumnAlignment("vlera",Table.ALIGN_RIGHT );
		
		return tarifaListTable;
	}

	private void setSasiaOnTarifaListTable(double s){
		
		List<TarifaFatura> col = (List<TarifaFatura>) this.tarifaFaturaContainer.getItemIds();
		
		Double totali = 0d;
		
		int i = 0;
		for (TarifaFatura tarifaFatura : col) {
			double sasia = 0;
			if (tarifaFatura.getNjesia() != null && tarifaFatura.getNjesia().toLowerCase().equals("m3")){
				sasia = s;
			}
			else{
				sasia = 1d;
			}
			tarifaFatura.setSasia(sasia);
			double vlera =  sasia * tarifaFatura.getCmimi();
			boolean tvsh = tarifaFatura.isAplikoTvsh();
			if (tvsh ){
				vlera = vlera + vlera*20/100; 
			}
			tarifaFatura.setVlera(vlera);
			totali += vlera;
			tarifaListTable.setColumnFooter("vlera", (new Double(totali)).toString() );
			
			col.set(i, tarifaFatura);
			
			i+=1;
			System.out.println("FaturaForm.setSasiaOnTarifaListTable()");
		}
		
		tarifaFaturaContainer.removeAllItems();
		tarifaFaturaContainer.addAll(col);
	
	}


	private final TextField shumaPaguar = new TextField("Shuma e paguar");
	
	private boolean mbyllFatureShtypur = false;
	private final FormLayout procesoFatureFormLayout = new FormLayout();
	
	private boolean anulloFatureShtypur = false;
	private final TextArea arsyeAnullimi =new TextArea("Arsye e anullimit");
	
	@Override
	public void buttonClick(ClickEvent event) {
		if (event.getComponent().equals(this.mbyllFature)){
			if (!mbyllFatureShtypur){
				shumaPaguar.setValue(fatura.getTotali());
				this.removeComponent(mbyllFature);
				procesoFatureFormLayout.removeAllComponents();
				procesoFatureFormLayout.addComponent(shumaPaguar);
				procesoFatureFormLayout.addComponent(this.mbyllFature);
				this.mbyllFatureShtypur = true;
				this.addComponent(procesoFatureFormLayout);
			}
			else {
				fatura.setPaguar(true);
				fatura.setShumaPaguar(Double.parseDouble(shumaPaguar.getValue().toString()));
				this.faturaBean.saveFatura(fatura);
				this.removeComponent(procesoFatureFormLayout);
				procesoFatureFormLayout.removeAllComponents();
				Window.Notification n = new Window.Notification("Info",
		                Window.Notification.TYPE_TRAY_NOTIFICATION);
		        n.setPosition(Window.Notification.POSITION_CENTERED);
		        n.setDescription("Fatura u mbyll!");
		        getWindow().showNotification(n);
		
			}
		}
		else if (event.getComponent().equals(this.anulloFature)){
			
			if (!anulloFatureShtypur){
				arsyeAnullimi.setValue("");
				
				this.removeComponent(anulloFature);
				procesoFatureFormLayout.removeAllComponents();
				procesoFatureFormLayout.addComponent(arsyeAnullimi);
				procesoFatureFormLayout.addComponent(this.anulloFature);
				this.anulloFatureShtypur = true;
				this.addComponent(procesoFatureFormLayout);
			}
			else {
				fatura.setAnulluar(true);
				fatura.setArsyeAnullimi(arsyeAnullimi.getValue().toString());
				this.faturaBean.saveFatura(fatura);
				this.removeComponent(procesoFatureFormLayout);
				procesoFatureFormLayout.removeAllComponents();
				Window.Notification n = new Window.Notification("Info",
		                Window.Notification.TYPE_TRAY_NOTIFICATION);
		        n.setPosition(Window.Notification.POSITION_CENTERED);
		        n.setDescription("Fatura u anullua!");
		        getWindow().showNotification(n);
		
			}
		}
		

	}

	
}
